import base64
import math
# text = "H 王博士"
# text_bytes = text.encode('utf-8')
# base64_bytes = base64.b64encode(text_bytes)
# base64_string = base64_bytes.decode('ascii')

# print(f"Original text: {text}")
# print(f"Base64 encoded string: {base64_string}")

# Decoding the Base64 string back to the original text
# base64_bytes_decoded = base64.b64decode(base64_string)
# text_decoded = base64_bytes_decoded.decode('utf-8')

# print(f"Decoded text: {text_decoded}")



def utf8_string_to_hex_uint8array(text):
    """
    Converts a UTF-8 string to a hex-encoded Uint8Array.

    Args:
        text: The UTF-8 string to convert.

    Returns:
        A string representing the hex-encoded Uint8Array.
    """
    utf8_bytes = text.encode('utf-8')
    hex_string = ''.join([f'{byte:02x}' for byte in utf8_bytes])
    return hex_string


###########################################
# Helper to encode the data characteristic UTF-8
def _encode_data(data):
    return str(data).encode('utf-8')

return_value = "{\"id\":\"master\",\"text\":\"《贺新郎·读史》人猿相揖别。只几个石头磨过，小儿时节。铜铁炉中翻火焰，为问何时猜得，不过几千寒热。人世难逢开口笑，上疆场彼此弯弓月。流遍了，郊原血。一篇读罢头飞雪，但记得斑斑点点，几行陈迹。五帝三皇神圣事，骗了无涯过客。有多少风流人物？盗跖庄蹻流誉后，更陈王奋起挥黄钺。歌未竟，东方白。\"}"
PAGE_SIZE = 20 # 分包大小(字节)

arraybuffer = _encode_data(return_value)
bytesLength = len(arraybuffer)

print(type(arraybuffer))
print(len(arraybuffer))

meta_chunks_number = math.ceil(bytesLength / PAGE_SIZE)  # 先进行浮点数除法 7/3 ≈ 2.333，再向上取整得到 3

print("meta_chunks_number =", meta_chunks_number)

# 标记点
flag_pointer = 0

while flag_pointer < bytesLength:
	
	
	
	next_flag_pointer = flag_pointer + PAGE_SIZE -1

	print("--\n")
	print(flag_pointer)
	print(next_flag_pointer)
	
	if next_flag_pointer < bytesLength:
		chunk = arraybuffer[flag_pointer:next_flag_pointer]
		print(chunk)
		
		
	else:
		chunk = arraybuffer[flag_pointer:bytesLength-1]
		print(chunk)
		pass
		
	flag_pointer += PAGE_SIZE
	print("flag_pointer = ", flag_pointer)
	
exit()

###########################################

mutable_bytearray = bytearray(b'')
print("mutable_bytearray's type =", type(mutable_bytearray))

text_string = "H 王博士"
hex_string = utf8_string_to_hex_uint8array(text_string)
string_bytes = bytes.fromhex(hex_string)
print("string_bytes's type =", type(string_bytes))

mutable_bytearray += string_bytes
result_bytes = bytes(mutable_bytearray)
print("result_bytes's type =", type(result_bytes))

string_china = result_bytes.decode('utf-8', 'ignore')
print(string_china)
exit()

###########################################

# Example usage
text_string = "H 王博士"
hex_string = utf8_string_to_hex_uint8array(text_string)
print(hex_string)

string_bytes = bytes.fromhex(hex_string)

string_china = string_bytes.decode('utf-8', 'ignore')

print(string_china)


